package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAreaMapper;
import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.service.ISysAreaService;

/**
 * 地区Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-07
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService 
{
    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询地区
     * 
     * @param areaId 地区主键
     * @return 地区
     */
    @Override
    public SysArea selectSysAreaByAreaId(Long areaId)
    {
        return sysAreaMapper.selectSysAreaByAreaId(areaId);
    }

    /**
     * 查询地区列表
     * 
     * @param sysArea 地区
     * @return 地区
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea)
    {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }

    /**
     * 新增地区
     * 
     * @param sysArea 地区
     * @return 结果
     */
    @Override
    public int insertSysArea(SysArea sysArea)
    {
        SysArea parentArea = sysAreaMapper.selectSysAreaByAreaId(sysArea.getParentId());
        sysArea.setCreateTime(DateUtils.getNowDate());
        sysArea.setAncestors(parentArea.getAncestors() + "," + sysArea.getParentId());
        return sysAreaMapper.insertSysArea(sysArea);
    }

    /**
     * 开通根地区
     * @param sysArea
     * @return
     */
    @Override
    public int insertRootSysArea(SysArea sysArea)
    {
        sysArea.setCreateTime(DateUtils.getNowDate());
        sysArea.setParentId(0L);
        sysArea.setAncestors("0");
        return sysAreaMapper.insertSysArea(sysArea);
    }

    /**
     * 判断部门是否存在地区
     * @param deptId
     * @return
     */
    @Override
    public boolean checkAreaByDeptId(Long deptId){
        SysArea area = new SysArea();
        area.setDeptId(deptId);
        area.setParentId(0L);
        return sysAreaMapper.selectSysAreaList(area).size() > 0;

    }

    /**
     * 修改地区
     * 
     * @param sysArea 地区
     * @return 结果
     */
    @Override
    public int updateSysArea(SysArea sysArea)
    {
        SysArea parentArea = sysAreaMapper.selectSysAreaByAreaId(sysArea.getParentId());
        SysArea oldArea = sysAreaMapper.selectSysAreaByAreaId(sysArea.getAreaId());
        if (StringUtils.isNotNull(parentArea) && StringUtils.isNotNull(oldArea)){
            String newAncestors = parentArea.getAncestors() + "," + parentArea.getAreaId();
            String oldAncestors = oldArea.getAncestors();
            sysArea.setAncestors(newAncestors);
            updateAreaChildren(sysArea.getAreaId(), newAncestors, oldAncestors);
        }
        sysArea.setUpdateTime(DateUtils.getNowDate());
        return sysAreaMapper.updateSysArea(sysArea);
    }

    public void updateAreaChildren(Long areaId, String newAncestors, String oldAncestors){
        List<SysArea> children = sysAreaMapper.selectChildrenAreaById(areaId);
        for (SysArea child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            sysAreaMapper.updateAreaChildren(children);
        }
    }

    /**
     * 批量删除地区
     * 
     * @param areaIds 需要删除的地区主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByAreaIds(Long[] areaIds)
    {
        return sysAreaMapper.deleteSysAreaByAreaIds(areaIds);
    }

    @Override
    public boolean hasChildByAreaId(Long areaId) {
        return sysAreaMapper.hasChildByAreaId(areaId) > 0;
    }

    /**
     * 删除地区信息
     * 
     * @param areaId 地区主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByAreaId(Long areaId)
    {
        return sysAreaMapper.deleteSysAreaByAreaId(areaId);
    }
}
